/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entity;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author artha
 */


public class KelolaPenyakit {
    public KelolaPenyakit() {
        emf = Persistence.createEntityManagerFactory("Dokter_mandiriPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void createPenyakit(Penyakit penyakit) {
        EntityManager em = getEntityManager();
        em.getTransaction().begin();
        try {
            em.persist(penyakit);
            em.getTransaction().commit();
        } catch (Exception e) {
            em.getTransaction().rollback();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }
    public Penyakit getPenyakit(String namaPenyakit) {
        Penyakit penyakit = null;
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("SELECT object(a) FROM Penyakit AS a WHERE a.namaPenyakit=:np");
            q.setParameter("np", namaPenyakit);
            penyakit = (Penyakit) q.getSingleResult();
        } finally {
            if (em != null) {
                em.close();
            }
        }
        return penyakit;
    }
    
}
